﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NLBS.Data;
using NLBS.Entity;
using NLBS.Web;

namespace NLBS.Control
{
    public class PanelArchive : PanelModule
    {
        [TemplateContainer(typeof(DefaultArchiveTemplate))]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public override ITemplate ItemTemplate
        {
            get
            {
                return base.ItemTemplate;
            }
            set
            {
                base.ItemTemplate = value;
            }
        }

        public override string Title
        {
            get
            {
                return "随笔归档";
            }
            set
            {
                base.Title = value;
            }
        }

        protected override void CreateChildControls()
        {
            if (ItemTemplate == null)
                ItemTemplate = new DefaultArchiveTemplate();
            base.CreateChildControls();
        }
    }

    public class DefaultArchiveTemplate : ITemplate
    {
        public void InstantiateIn(System.Web.UI.Control container)
        {
            container.Controls.Add(new LiteralControl("<ul>"));
            IList<Archive> archiveList = Cacher.GetArticleArchive();
            if (archiveList != null)
            {
                foreach (Archive archive in archiveList)
                {
                    container.Controls.Add(new LiteralControl("<li>"));
                    string txt = string.Format("<a href=\"{0}\"  onclick=\"{1}\">{2}</a>",
                        "javascript:void(0)",
                        UrlFormatsProvider.Instance().ArticleDateUrl(archive.Date.ToString("yyyy-MM")),
                        archive.Date.ToString("yyyy年MM月") + "(" + archive.Count.ToString() + ")");
                    container.Controls.Add(new LiteralControl(txt));
                    container.Controls.Add(new LiteralControl("</li>"));
                }
            }
            container.Controls.Add(new LiteralControl("</ul>"));
        }
    }
}
